import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

@WebServlet("/showRequest")
public class ShowRequestServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       // super.doGet(req, resp);

        // reps 是响应对象，setContentType给响应的ContentType设置了值，html类型
        // 说明响应body是html结构的数据，此时\n就不太好使了！！
        // 所以此时就将 \n全部改为<br>!!!!
        resp.setContentType("text/html");

        StringBuilder stringBuilder = new StringBuilder();
        // getProtocol(): 协议名/版本号
        stringBuilder.append(req.getProtocol());
        stringBuilder.append("<br>");
        stringBuilder.append(req.getMethod());
        stringBuilder.append("<br>");
        stringBuilder.append(req.getRequestURI());
        stringBuilder.append("<br>");
        stringBuilder.append(req.getContextPath());
        stringBuilder.append("<br>");
        stringBuilder.append(req.getQueryString());
        stringBuilder.append("<br>");

        // 把请求的header也拼进来
        Enumeration<String> headerName = req.getHeaderNames();
        while (headerName.hasMoreElements()) {
            String name = headerName.nextElement();
            String value = req.getHeader(name);
            stringBuilder.append(name+":"+value);
            stringBuilder.append("<br>");
        }

        resp.getWriter().write(stringBuilder.toString());
    }
}
